package com.jhws.business.comm.api;

import com.jhws.business.comm.bo.Comm;
import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/v3/commInfo")
public class CommApiV3 extends commApiV2 {

    @ResponseBody
    @RequestMapping({"anon/commList", "commList"})
    public JsonRespData getCommByCity(String city) throws Exception {
        if (StringUtils.isEmpty(city)) {
            return new JsonRespData("", -1, "城市必须填写！");
        }
        String where = " where ";
       /* 演示虚拟小区，代表test属性小区 */
        if (city.equals("演示")) {
            where += "comm_state='test'";
        } else {
            where += " comm_state='deploy' and ";
            if (!city.equals("")) {
                where += "comm_city like '%" + city + "%' ";
            }
        }
        List<TblCommDimBo> tblCommDimBos = ICommService.findTblCommDimByWhere(where);
        if (tblCommDimBos.size() > 0) {
            List<Comm> comms = new ArrayList<>();
            Comm comm;
            for (TblCommDimBo tblCommDimBo : tblCommDimBos) {
                comm = new Comm();
                comm.setCommCode(tblCommDimBo.getCommCode());
                comm.setCity(tblCommDimBo.getCity());
                comm.setCommName(tblCommDimBo.getCommName());
                comm.setManageDevNum(tblCommDimBo.getManageDevNum());
                comm.setGateDevNum(tblCommDimBo.getGateDevNum());
                comms.add(comm);
            }
            return new JsonRespData("", 1, comms);
        } else {
            return new JsonRespData("", -1, "没有可选择小区");
        }
    }
}
